var imgFile,ue;
var datalist;
$(function () {
    ue =new UE.ui.Editor({
        initialFrameHeight:300,initialFrameWidth:675
    });
    ue.render("editor");
    getMune();
});
/*重置*/
function resetClick(){
    $(".dis-inebl input").val("");
    $("#menus option:selected").val("全部");
    $("#userState").find("option[value = '1']").prop("selected","selected");
    getUserList();
}
//查询菜单
function getMune(){
    JY.Model.loading();
    $.ajax({
        url : jypath + "/menu/getAllMenuTag",
        type : "get",
        jsonType : "json",
        success : function(data) {
            var html="";
            for(var i=0;i<data.obj.length;i++){
                var l=data.obj[i];
               html+="<option value='"+l.menuTagId+"'>"+l.menuTagName+"</option>"
            }
            $("#menus").append(html);
            $("#menuName").append(html);
            $("#menuName").find("option[value = '10']").prop("selected","selected");
            JY.Model.loadingClose();
        }
    })
    getUserList();
}
/*查询*/
function getUserList(init){
    JY.Model.loading();
    var pageNum;
    var pageSize
    if(init!=1){
        pageNum  =$("#baseForm .pageNum").val();
        pageSize  =$("#baseForm .pageSize").val();
    }
	$("#ticktTable tbody").html("");
	var artName =$("#artName").val().trim();
	var userState=$("#userState option:selected").val();
    var menus=$("#menus option:selected").val();
    if(menus=="全部"){
        menus="";
    }
	$.ajax({
		url : jypath + "/article/getArticlesParameter",
		type : "get",
		data:{
            "pageNum":pageNum,
            "pageSize":pageSize,
            "menuTagId":menus,
            "articleTitle":artName,
            "state":userState,
		},
		jsonType : "json",
		success : function(data) {
            var data=data instanceof Object ? data : JSON.parse(data);
            var html="";
            var list=data.obj;
            datalist=data.obj;
            var results=list.results;
            var pageNum=list.pageNum,
                pageSize=list.pageSize,
                totalRecord=list.totalRecord;
           if(results!=null&&results.length>0){
                var leng=(pageNum-1)*pageSize;//计算序号
                for(var i = 0;i<results.length;i++){
                    var l=results[i];
                    var jsonL=JSON.stringify(l);
                    html+="<tr class='tr_color'>";
                    html+="<td class='center ver-m'><label><input type='checkbox' data_index='"+i+"' name='ids' value='"+jsonL+"' class='ace' /> <span class='lbl'></span></label></td>";
                    html+="<td class='center'>"+l.articleTitle+"</td>";
                    html+="<td class='center'>"+l.menuName+"</td>";
                    if(l.recommendedArticles==1){
                        html+="<td class='center'>推荐</td>";
                    }else if(l.recommendedArticles==0){
                        html+="<td class='center'>否</td>";
                    }
                    if(l.state==1){
                        html+="<td class='center'>有效</td>";
                    }else if(l.state==0){
                        html+="<td class='center'>无效</td>";
                    }
                    html+="</tr>";
                }
                $("#ticktTable tbody").append(html);
                JY.Page.setPage("baseForm","pageing",pageSize,pageNum,totalRecord,"getUserList");
                $("#ticktTable .tr_color").click(function(){
                    $(this).addClass("bgf5").siblings(".tr_color").removeClass("bgf5");
                    if($(this).find(".id_color").hasClass("bg6FAED9")){
                        $(this).find(".id_color").removeClass("bg6FAED9")
                    }else{
                        $(this).find(".id_color").addClass("bg6FAED9").parents(".tr_color").siblings(".tr_color").find(".id_color").removeClass("bg6FAED9");
                    }
                });
            }else{
                html+="<tr><td colspan='7' class='center'>没有相关数据</td></tr>";
                $("#ticktTable tbody").append(html);
                $("#baseForm ul").empty();//清空分页
            }
            JY.Model.loadingClose();
		}
	})
}
//图片预览
function preview(obj) {
    var url= window.URL.createObjectURL(obj.files[0]);
    $(obj).prev().attr("src",url);
    imgFile=obj.files[0];
}
//新增
function addAri(){
    clearForm();
    JY.Model.edit("auForm_edit","新增文章",function(){
        var that=$(this);
        var cont=UE.getEditor('editor').getContent();
        if(JY.Validate.form("generateForm")){
            if($(".preview_ing").attr("src")==""+jypath+"/static/images/back/add_img.jpg"){
                JY.Model.info("请先新增封面图片!");
            }else{
                $(".masks").removeClass("hide");
                $.ajax({
                    url : jypath + "/article/insertArticle",
                    type : "post",
                    data:{
                        "articleTitle":$("#generateForm input[name='articleTitle']").val(),
                        "menuTagId":$("#menuName").find("option:selected").val(),
                        "recommendedArticles":$("#groomsSel").find("option:selected").val(),
                        "articleDescribe":$("#generateForm .des").val(),
                        "articleDatail":cont,
                    },
                    success : function(data) {
                        if(data.res==1){
                            var relevanceId=data.obj;
                            var formData=new FormData();
                            formData.append("imageFile",imgFile);
                            formData.append("relevanceId",relevanceId);
                            formData.append("relevanceTpyeId",1);
                            formData.append("relevanceTpyeDetail",0);
                            $.ajax({
                                url: jypath + "/uploadVideoAndImage",
                                type: "post",
                                processData: false,
                                contentType: false,
                                data: formData,
                                success: function (data) {
                                    that.dialog("close");
                                    $(".masks").addClass("hide");
                                    JY.Model.info("操作成功!",function(){getUserList();});
                                }
                            })
                        }else{
                            that.dialog("close");
                            $(".masks").addClass("hide");
                            JY.Model.info("添加失败!");
                        }
                    }
                })
            }
        }
    })
}
// 启用禁用
function enAbleClick(type){
    var chks =[];
    $('#ticktTable input[name="ids"]:checked').each(function(){
        var jsonV=JSON.parse($(this).val());
        chks.push(jsonV.articleId);
    });
    if(chks.length==0) {
        JY.Model.info("您没有选择任何内容!");
    }else if(chks.length>0){
        var states="";
        if(type==1){
            //启用
            states=1;
        }else if(type==0){
            //禁用
            states=0;
        }
        console.log(chks);
        JY.Model.message("auForm_edits","修改状态",function(){
            var that = $(this);
            $.ajax({
                url : jypath + "/article/updateArticleState",
                type : "POST",
                data:{
                    "state":states,
                    "articleId":chks.join()
                },
                jsonType : "json",
                success : function(data) {
                   if(data.res==1){
                       that.dialog("close");
                       JY.Model.info("操作成功!",function(){getUserList();});
                   }
                }
            })
        });
    }
}
//编辑
function editAri(){
    clearForm();
    var chks =[];
    $('#ticktTable input[name="ids"]:checked').each(function(){
       var i= $(this).attr("data_index");
        chks.push( datalist.results[i]);
    });
    if(chks.length==0) {
        JY.Model.info("您没有选择任何内容!");
    }else if(chks.length==1){
        var obj=chks[0];
        $("#generateForm input[name='articleTitle']").val(obj.articleTitle);
        $("#menuName").find("option[value = '"+obj.menuTagId+"']").prop("selected","selected");
        $("#groomsSel").find("option[value = '"+obj.recommendedArticles+"']").prop("selected","selected");
        $("#generateForm .preview_ing").attr("src",obj.articleCoverUlr);
        $("#generateForm textarea[name='des']").val(obj.articleDescribe);
        ue.setContent(obj.articleDatail);
        ue.addListener('ready', function (editor) {
            ue.setContent(obj.articleDatail);
        });
        JY.Model.message("auForm_edit","编辑",function(){
            $(".masks").removeClass("hide");
            var that = $(this);
            var cont=UE.getEditor('editor').getContent();
            if(imgFile.name == undefined){
                $.ajax({
                    url : jypath + "/article/updateArticle",
                    type : "POST",
                    data:{
                        "articleId":obj.articleId,
                        "articleTitle":$("#generateForm input[name='articleTitle']").val(),
                        "menuTagId":$("#menuName").find("option:selected").val(),
                        "recommendedArticles":$("#groomsSel").find("option:selected").val(),
                        "articleDescribe":$("#generateForm .des").val(),
                        "articleDatail":cont,
                    },
                    jsonType : "json",
                    success : function(data) {
                        if(data.res==1){
                            that.dialog("close");
                            $(".masks").addClass("hide");
                            JY.Model.info("操作成功!",function(){getUserList();});
                        }
                    }
                })
            }else{
                $.ajax({
                    url : jypath + "/article/updateArticle",
                    type : "POST",
                    data:{
                        "articleId":obj.articleId,
                        "articleTitle":$("#generateForm input[name='articleTitle']").val(),
                        "menuTagId":$("#menuName").find("option:selected").val(),
                        "recommendedArticles":$("#groomsSel").find("option:selected").val(),
                        "articleDescribe":$("#generateForm .des").val(),
                        "articleDatail":cont,
                    },
                    jsonType : "json",
                    success : function(data) {
                        if(data.res==1){
                            if(data.res==1){
                                var relevanceId=obj.articleId;
                                var formData=new FormData();
                                formData.append("imageFile",imgFile);
                                formData.append("relevanceId",relevanceId);
                                formData.append("relevanceTpyeId",1);
                                formData.append("relevanceTpyeDetail",0);
                                $.ajax({
                                    url: jypath + "/uploadVideoAndImage",
                                    type: "post",
                                    processData: false,
                                    contentType: false,
                                    data: formData,
                                    success: function (data) {
                                        that.dialog("close");
                                        $(".masks").addClass("hide");
                                        JY.Model.info("操作成功!",function(){getUserList();});
                                    }
                                })
                            }else{
                                that.dialog("close");
                            }
                        }
                    }
                })
            }

        });
    }else if(chks.length>1){
        JY.Model.info("一次只能编辑一条信息!");
    }
}
function clearForm(){
    $("#generateForm input[name='articleTitle']").val("");
    $("#menuName").find("option[value = '10']").prop("selected","selected");
    $("#groomsSel").find("option[text = '否']").prop("selected","selected");
    $("#generateForm .preview_ing").attr("src",""+jypath+"/static/images/back/add_img.jpg");
    $("#generateForm textarea[name='doctorRealName']").val("");
    $("#input1").val("");
    UE.getEditor('editor').setContent('', false);
    imgFile={};
}